package com.itboye.ihomebank.activity.key.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.activity.keytwo.KeyShouQuanActivity;
import com.itboye.ihomebank.adapter.AdminKeyAdapter;
import com.itboye.ihomebank.adapter.YongHuGuanLiAdapter;
import com.itboye.ihomebank.base.BaseFragment;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.AdminKeyBean;
import com.itboye.ihomebank.bean.KeyOfLockBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.activity.key.ActivityKaiSuoJiLu;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 锁用户管理---钥匙
 */
public class FragmentGuanLiKey extends BaseFragment implements Observer {
    XListView key_listview;
    YongHuGuanLiAdapter adapter;
    KeyOfLockBean bean;
    private List<KeyOfLockBean.ListKey> beans;
    UserPresenter userPresenter;
    private String uid;
    private String lockId;
    @Override
    public int initView() {
        return R.layout.activity_guan_li_key;
    }

    @Override
    public void onMyActivityCreated() {
        key_listview.setPullRefreshEnable(false);
        userPresenter = new UserPresenter(this);

        uid = SPUtils.get(getActivity(), null, SPContants.USER_ID, "") + "";
        lockId = getActivity().getIntent().getStringExtra("lockid");

    }

    @Override
    public void onResume() {
        super.onResume();
        beans = new ArrayList<>();
        String sid= SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";
        boolean islock= (boolean) SPUtils.get(MyApplcation.ctx,null,SPContants.ISLOCK,false);
        if (islock){
            userPresenter.keyOfLock(sid, lockId);
        }else {
            userPresenter.keyOfLock(uid, lockId);
        }

    }

    @Override
    public void update(Observable observable, Object data) {
        // TODO Auto-generated method stub
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == UserPresenter.keyOfLock_success) {
                bean = (KeyOfLockBean) entity.getData();
                if (beans != null) {
                    Log.d("======guanl",beans.toString());
                    beans.addAll(bean.getList());
                    adapter = new YongHuGuanLiAdapter(getActivity(), beans,
                            R.layout.item_yonghu_guanli);
                    beans.get(0).setMark(bean.getLock_alias());
                    key_listview.setAdapter(adapter);

                    key_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) {
                            // TODO Auto-generated method stub

                            try {
                                if (beans.get(position - 1).getType().equals("0")){//管理员不能进入自己的钥匙
                                    ByAlert.alert("管理员钥匙");
                                    return;
                                }
                                Intent intent = new Intent(getActivity(),
                                        KeyShouQuanActivity.class);
                                intent.putExtra("lockid", beans.get(position - 1).getLock_id());
                                intent.putExtra("touid", beans.get(position - 1).getUid());
                                intent.putExtra("keyid", beans.get(position - 1).getKey_id());
                                intent.putExtra("status", beans.get(position - 1).getStatus());
                                intent.putExtra("user_type", beans.get(position - 1).getType());
                                intent.putExtra("user_type", beans.get(position - 1).getType());
                                intent.putExtra("start", beans.get(position - 1).getStart());
                                intent.putExtra("end", beans.get(position - 1).getEnd());
                                startActivity(intent);
                            }catch (Exception e){

                            }

                        }
                    });
                }
            } else if (entity.getEventType() == UserPresenter.keyOfLock_fail) {
                ByAlert.alert(entity.getData());
            }
        }
    }
}
